<template>
    <transition name="el-message-fade">
        <div class="notice" v-show="visible">
            <div class="icon">
                <i class="iconfont icon-icon-warning"></i>
            </div>
            <div class="content">
                {{ content }}
            </div>
        </div>
    </transition>
</template>

<script>
    export default {
        name: 'notice',
        data() {
            return {
                visible: false,
                content: '',
                duration: 1000
            }
        },
        methods: {
            setTimer() {
                setTimeout(() => {
                    this.close() // 3000ms之后调用关闭方法
                }, this.duration)
            },
            close() {
                this.visible = false
                setTimeout(() => {
                    this.$destroy(true)
                    this.$el.parentNode.removeChild(this.$el) // 从DOM里将这个组件移除
                }, 500)
            }
        },
        mounted() {
            this.setTimer() // 挂载的时候就开始计时，3000ms后消失
        }
    }
</script>

<style lang="scss">
    .notice {
        min-width: 380px;
        background: #ebeef5;
        left: 50%;
        position: fixed;
        top: 20px;
        transform: translateX(-50%);
        padding: 15px 15px 15px 20px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        color: #909399;
        .icon {
            margin-right: 10px;
        }
    }
    .el-message-fade-enter,
    .el-message-fade-leave-active {
        opacity: 0;
        transform: translate(-50%, -100%);
    }
    
</style>